<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
  <HEAD>
    <TITLE> New Document </TITLE>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <%= stylesheet_link_tag "../javascripts/ext/resources/css/ext-all.css" %>
    <%= stylesheet_link_tag "/stylesheets/tasks.css" %>

    <%= javascript_include_tag "ext/adapter/ext/ext-base.js" %>
    <%= javascript_include_tag "ext/ext-all.js" %>
    <%= javascript_include_tag "ext/build/locale/ext-lang-zh_CN.js" %>
    <script type="text/javascript">
      var metawork={};
      metawork.searchFieldsCache = new Object();
      var dataFields = [
        {
          id:"title",
          name:"标题",
          type:"string"
        },{
          id:"id",
          name:"标识",
          type:"int"
        },{
          id:"organization",
          name:"所属机构",
          type:"",
          children:[          //代表子属性
            {
              id:"id",
              name:"标识",
              type:"int"
            },{
              id:"id",
              name:"名称",
              type:"string"
            }
          ]
        }
      ];
  
      var fieldsEditor = new Ext.form.ComboBox({
        store:new Ext.data.SimpleStore({fields:[],data:[[]]}),
        displayField:'name',
        mode: 'local',
        editable:false,
        triggerAction: 'all',
        tpl: '<div id="fieldsMenu"></div>'
      });
  
  
  
      var fieldMenu = new Ext.menu.Menu({
        id: 'fieldsRootMenu',
        minWidth:150
      });
 
      Ext.menu.DynamicItem = function(config){
        Ext.menu.DynamicItem.superclass.constructor.call(this, config);
        this.on("click",function(m,e){
          fieldsEditor.setValue(m.fullText||m.text);
        });
      };
  
      Ext.extend(Ext.menu.DynamicItem, Ext.menu.Item, {
        // private
        expandMenu : function(autoActivate){
          if(!this.disabled && this.menu){
            clearTimeout(this.hideTimer);
            delete this.hideTimer;
            if(!this.menu.isVisible() && !this.showTimer){
  
              //如果有chldren属性并且还没有装载，那么装载之
              if(this.children && this.children.length>0 && this.menu.items.length == 0){
                for(var index=0;index<this.children.length;index++){
                  var item = this.children[index];
                  this.menu.add(new Ext.menu.DynamicItem({
                    text:item.name,
                    menu:item.children?{items:[]}:null,
                    children:item.children,
                    fullText:this.text + "_" + item.name,
                    url:item.url
                  }));
                }
              }
  
              this.showTimer = this.deferExpand.defer(this.showDelay, this, [autoActivate]);
            }else if (this.menu.isVisible() && autoActivate){
              this.menu.tryActivate(0, 1);
            }
          }
        }
      });
  
      for(var i = 0;i<dataFields.length;i++){
        var item = dataFields[i];
        fieldMenu.add(new Ext.menu.DynamicItem({
          text:item.name,
          menu:item.children?{items:[]}:null,
          children:item.children
        }));
      }
 
 
      fieldsEditor.on('expand',function(){
        fieldMenu.show(Ext.get("fieldsMenu"));
      });

    </script>
  </HEAD>

  <BODY onload="fieldsEditor.render('fieldEditor')">
    <div id="fieldEditor"></div>
  </BODY>
</HTML>